package com.xl.web.controller.zoom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.StringUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.xl.base.annotation.Auth;
import com.xl.base.page.Page;
import com.xl.base.page.PageBean;
import com.xl.base.web.BaseController;
import com.xl.core.util.HtmlUtil;
import com.xl.web.entity.zoom.Group;
import com.xl.web.service.zoom.GroupService;

@Controller
@Scope("prototype")
@RequestMapping("/group")
public class GroupController extends BaseController {
	@Autowired(required = false)
	private GroupService<Group> groupService;
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value="/queryPages",method=RequestMethod.GET)
	@Auth(verifyLogin = false)
	/**
	 * 查询当前用户使用的组及用户列表信息
	 * @param user
	 * @throws Exception
	 */
	public void queryPageGroupUser(Group group,Page page) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		PageBean pageBean  = groupService.queryPageGroupUser(group,page);
	 	    result.put(this.SUCCESS,true);
		    result.put(this.MSG, "查询成功!");
		    result.put("data",pageBean);
		HtmlUtil.writerJson(response, result);
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/queryUsers",method=RequestMethod.GET)
	@Auth(verifyLogin = false)
	/**
	 * 查询当前用户使用的组及用户列表信息
	 * @param user
	 * @throws Exception
	 */
	public void queryGroupUser(Group group) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
	 	List<Group> groupUserList = groupService.queryGroupUser(group);
	 	    result.put(this.SUCCESS, true);
		    result.put(this.MSG, "查询成功!");
		    result.put("count", groupUserList!=null?groupUserList.size():0);
		    result.put("data",groupUserList);
		HtmlUtil.writerJson(response, result);
	}
	//添加组信息
	@ResponseBody
	@RequestMapping(value="/create",method=RequestMethod.POST)
	@Auth(verifyLogin = false)
	public void addGroup(Group group)throws Exception{
		String msg = "保存成功!";
		boolean flag = true;
	 	if(StringUtil.isEmpty(group.getGroupName()+"")  ){
			msg = "分组名称不能为空";
			flag = false;
			this.sendFailureMessage(response, msg);
			return;
		} 
		 if(StringUtil.isEmpty(group.getCreateUserId()+"")  ){
			msg = "创建用户ID不能为空";
			flag = false;
			this.sendFailureMessage(response, msg);
			return;
		}
	
		
		List<Group> groupUserList = groupService.queryGroupUser(group);
		 if(groupUserList!=null && groupUserList.size()>0){
			msg="在当用户下已存在【"+group.getGroupName()+"】分组名称，不能重复添加！";
			flag = false;
			this.sendFailureMessage(response, msg);
			return;
		}
		if(flag){
			groupService.saveGroup(group);
			this.sendSuccessMessage(response, msg);
		}else{
			this.sendFailureMessage(response, msg);
		} 
	}
	   //删除组
		@ResponseBody
		@RequestMapping(value="/delete",method=RequestMethod.POST)
		@Auth(verifyLogin = false)
		public void delGroup(Group group)throws Exception{
			String msg = "删除成功!";
			boolean flag = true;
			 if(!"".equals(group.getGroupId())&&group.getGroupId()>0){
				 //先查询该分组下是否有用户有用户则不能被删除
				List<Group> groupUserList = groupService.queryGroupUser(group);
				if(groupUserList!=null && groupUserList.size()>0){
					//该分组下对应有用户，提示先要删除该组下的用户方可删除分组信息
					if(groupUserList.get(0).getJoinUserList()!=null && groupUserList.get(0).getJoinUserList().size()>0){
						msg ="该分组下有添加有用户数据，先删除该分组下的用户，然后再删除分组！";
						flag = false;
						this.sendFailureMessage(response, msg);
						return;
					}
				}else{
				    	msg ="要删除的分组不存在！";
				    	flag = false;
				    	this.sendFailureMessage(response, msg);
				    	return;
				}
			}else{
				msg ="删除的groupId不能为空！";
				flag = false;
				this.sendFailureMessage(response, msg);
				return;
			} 
			 if(flag){
				 groupService.delGroup(group);
				 this.sendSuccessMessage(response, msg);
			 }else{
				 this.sendFailureMessage(response, msg);
			 }
			
		}
		 //更新组信息  
		@ResponseBody
		@RequestMapping(value="/edit",method=RequestMethod.POST)
		@Auth(verifyLogin = false)
		public void editGroup(Group group)throws Exception{
			if(StringUtil.isEmpty(group.getGroupName()) ){
				this.sendFailureMessage(response, "更新失改，要更新的分组名称不能为空！");
				return;
			}
			//检查同一用户下是否已存在该分组，若存在则不能更新
			Group group1 = new Group();
			group1.setGroupId(group.getGroupId());
			List<Group> groupUserList = groupService.queryGroupUser(group1);
			if(groupUserList!=null && groupUserList.size()>0){
				//获取当前要修改的分组的创建人
				long createUserId = groupUserList.get(0).getCreateUserId();
				group1 = new Group();
				//根据当前创建人和分组名称，查询修改后的分组名称在当前用户下是否存在，存在则不能更新
				group1.setCreateUserId(createUserId);
				group1.setGroupName(group.getGroupName());
				List<Group> hasGroupNameList = groupService.queryGroupUser(group1);
				if(hasGroupNameList!=null && hasGroupNameList.size()>0){
					this.sendFailureMessage(response, "更新失改，分组名称【"+group.getGroupName()+"】在当前用户下已存在，不能在同一用户下有相同的分组名称！");
					return;
				}
			}else{
				this.sendFailureMessage(response, "更新失改，当前没有要更新的分组信息！");
				return;
			}
			groupService.editGroup(group);
			this.sendSuccessMessage(response, "更新成功！");
		}
		/**
		 * 
		 * @param param {groupId:1,userIdList:[1,2,3,4]}  
		 * @throws Exception
		 */
		 //添加用户组对应用户   
			@ResponseBody
			@RequestMapping(value="/saveGroupUser",method=RequestMethod.POST)
			@Auth(verifyLogin = false)
			public void saveGroupUser(@RequestBody String param)throws Exception{	
				JSONObject json = JSONObject.parseObject(param);
				if(!json.containsKey("groupId") || "".equals(json.getString("groupId")) ){
					this.sendFailureMessage(response, "添加失改，用户分组ID不能为空！！");
					return;
				}
				if(!json.containsKey("userIdList") || json.getJSONArray("userIdList").size()==0){
					this.sendFailureMessage(response, "添加失改，成员ID不能为空！");
					return;
				}
				groupService.saveGroupUser(json);
				this.sendSuccessMessage(response, "分组添加成员成功！"); 
				
			}
		/**
		 * 
		 * @param param {groupId:1,userIdList:[1,2,3,4]}   userIdList可为空，为空时，清空该组下所有用户
		 * @throws Exception
		 */
			//删除用户组对应用户
			@ResponseBody
			@RequestMapping(value="/delGroupUser",method=RequestMethod.POST)
			@Auth(verifyLogin = false)
			public void delGroupUser(@RequestBody String param)throws Exception{	
				JSONObject json = JSONObject.parseObject(param);
				if(!json.containsKey("groupId") || "".equals(json.getString("groupId")) ){
					this.sendFailureMessage(response, "删除失改，用户分组ID不能为空！！");
					return;
				}
				groupService.delGroupUser(json);
				this.sendSuccessMessage(response, "分组删除成员成功！"); 
				
			}
}
